package com.yxq.mdt.app.modules.order.ui;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseTabActivity;
import com.yxq.mdt.app.base.BaseTabBean;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.order.bean.EventOrderMsg;
import com.yxq.mdt.app.modules.order.bean.ExpressBean;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;
import com.yxq.mdt.app.modules.order.bean.OrderDetailBean;
import com.yxq.mdt.app.modules.order.bean.OrderListBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultBalanceBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultWxBean;
import com.yxq.mdt.app.modules.order.bean.OrderSubmitResult;
import com.yxq.mdt.app.modules.order.model.OrderModel;
import com.yxq.mdt.app.modules.order.prestener.IOrderContract;
import com.yxq.mdt.app.modules.order.prestener.OrderPresenter;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderAfterSaleListFragment;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderAllListFragment;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderFinishListFragment;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderObligationListFragment;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderReceiveListFragment;
import com.yxq.mdt.app.modules.order.ui.fragment.OrderToSendListFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author L
 * @time
 * @describe 我的订单
 */
public class OrderListTabActivity extends BaseTabActivity<BaseTabBean, Fragment, OrderPresenter> implements IOrderContract.View {

    List<Fragment> fragmentList;
    OrderAllListFragment orderAllListFragment;
    OrderObligationListFragment orderObligationListFragment;
    OrderToSendListFragment orderToSendListFragment;
    OrderReceiveListFragment orderReceiveListFragment;
    OrderFinishListFragment orderFinishListFragment;
    OrderAfterSaleListFragment orderAfterSaleListFragment;
    List<BaseTabBean> tabBeanList;
    String orderId;
    String price;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_list_tab;
    }

    @Override
    public List getTabList() {
        fragmentList = new ArrayList<>();
        orderAllListFragment = new OrderAllListFragment();
        orderObligationListFragment = new OrderObligationListFragment();
        orderToSendListFragment = new OrderToSendListFragment();
        orderReceiveListFragment = new OrderReceiveListFragment();
        orderFinishListFragment = new OrderFinishListFragment();
        orderAfterSaleListFragment = new OrderAfterSaleListFragment();

        fragmentList.add(orderAllListFragment);
        fragmentList.add(orderObligationListFragment);
        fragmentList.add(orderToSendListFragment);
        fragmentList.add(orderReceiveListFragment);
        fragmentList.add(orderFinishListFragment);
        fragmentList.add(orderAfterSaleListFragment);
        tabBeanList = new ArrayList<>();
        tabBeanList.add(new BaseTabBean(0, "全部", 0));
        tabBeanList.add(new BaseTabBean(1, "待付款", 1));
        tabBeanList.add(new BaseTabBean(2, "待发货", 2));
        tabBeanList.add(new BaseTabBean(3, "待收货", 3));
        tabBeanList.add(new BaseTabBean(4, "已完成", 4));
        tabBeanList.add(new BaseTabBean(5, "售后", 5));
        return tabBeanList;
    }

    @Override
    public Fragment getFragment(BaseTabBean tab) {
        Fragment fragment = fragmentList.get(tab.tabId);
        return fragment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //设置首选项字体颜色大小
        TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(0)).getChildAt(1));
        title.setTextColor(getResources().getColor(R.color.color_font_red));
        title.setTextAppearance(mActivity, Typeface.BOLD);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置选中字体颜色大小
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                title.setTextColor(getResources().getColor(R.color.color_font_red));
                title.setTextAppearance(mActivity, Typeface.BOLD);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //设置未选中字体颜色大小
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                title.setTextColor(getResources().getColor(R.color.color_font_black_666));
                title.setTextAppearance(mActivity, Typeface.NORMAL);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mCommonTitleBar.setListener((v, action, extra) -> {
            switch (action) {
                case CommonTitleBar.ACTION_LEFT_BUTTON:
                    finish();
                    break;
            }
        });
        mViewPager.setOffscreenPageLimit(6);
        mViewPager.setCurrentItem(getIntent().getIntExtra("tab", 0));
        mPresenter = new OrderPresenter(this, new OrderModel());
        EventBus.getDefault().register(this);

        orderId = getIntent().getStringExtra("orderId");
        price = getIntent().getStringExtra("price");
        becomeTeamer();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getBooleanExtra("gift", false)) {
            giftPackageSuccessfulDialog();
        }
    }
    /**
     * 购买大礼包后成为团长
     */
    private void becomeTeamer() {
        //判断是购买礼包商品
        if (getIntent().getBooleanExtra("gift", false)) {
            giftPackageSuccessfulDialog();
            //请求成为团长接口
//            H5Model.becomeTeamer(orderId, price, new ApiModel.CommonCallback<String>() {
//                @Override
//                public void onSuccess(String s) {
//                    giftPackageSuccessfulDialog();
//                }
//
//                @Override
//                public void onError(String errMsg) {
//                    ToastUtils.showShort(errMsg);
//                }
//            });
        }
    }

    /**
     * 成为团长弹出框
     */
    private void giftPackageSuccessfulDialog() {
        CostumerDialog dialog = new CostumerDialog.Builder(this, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_gift_package_layout)
                .setCancelable(true)
                .create();

        TextView title = dialog.findViewById(R.id.tv_title);

        title.setText("");

        TextView content = dialog.findViewById(R.id.tv_content);
        content.setText("恭喜您 已成为团长");

        //继续
        TextView sure = dialog.findViewById(R.id.tv_cancel);
        sure.setText("知道了");
        sure.setOnClickListener(v -> {
            dialog.dismiss();
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(EventOrderMsg message) {

        //刷新所有订单
        if (orderAllListFragment != null) {
            orderAllListFragment.initData();
        }

        switch (message.code) {

            case 1:
                if (orderObligationListFragment != null) {
                    orderObligationListFragment.initData();
                }
                break;
            case 2:
                if (orderToSendListFragment != null) {
                    orderToSendListFragment.initData();
                }
                break;
            case 3:
                if (orderReceiveListFragment != null) {
                    orderReceiveListFragment.initData();
                }
                break;
            case 4:
                if (orderFinishListFragment != null) {
                    orderFinishListFragment.initData();
                }
                break;
            case 5:
                if (orderAfterSaleListFragment != null) {
                    orderAfterSaleListFragment.initData();
                }
                break;
        }
    }

    /**
     * 选中某一页
     *
     * @param tab
     */
    public void setTab(int tab) {
        if (tab < mViewPager.getAdapter().getCount()) {
            mViewPager.setCurrentItem(tab);
        }
    }

    @Override
    public void onLoadSuccess(OrderConfirmBean beanList) {

    }

    @Override
    public void onLoadFailed(String err) {

    }

    @Override
    public void onLoadListSuccess(OrderListBean orderListBean) {

    }

    @Override
    public void onLoadListFailed(String err) {

    }

    @Override
    public void onSubmitSuccess(OrderSubmitResult orderSubmitResult) {

    }

    @Override
    public void onSubmitFailed(String err) {

    }

    @Override
    public void onPayBalanceSuccess(OrderPayResultBalanceBean orderPayResultBalanceBean) {

    }

    @Override
    public void onPayBalanceFailed(String err) {

    }

    @Override
    public void onPayWxSuccess(OrderPayResultWxBean orderPayResultWxBean) {

    }

    @Override
    public void onPayWxFailed(String err) {

    }

    @Override
    public void onDetailSuccess(OrderDetailBean listBean) {

    }

    @Override
    public void onDetailFailed(String err) {

    }

    @Override
    public void onExpressSuccess(ExpressBean expressBean) {

    }

    @Override
    public void onExpressFailed(String err) {

    }
}
